package com.ywm.api.user;

import com.alibaba.fastjson.JSON;
import com.ywm.model.user.User;
import com.ywm.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

	private static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;

	@PostMapping("/getUser")
	public String getUser(HttpServletRequest request) {
		Long id = ServletRequestUtils.getLongParameter(request, "id", 0L);
		User user = userService.getUserById(id);
		return JSON.toJSONString(user);
	}

	@PostMapping("/getUserList")
	public String getUserList(HttpServletRequest request) {
		Integer age = ServletRequestUtils.getIntParameter(request, "age", 0);
		List<User> list = userService.findList(age);
		logger.info("ok");
		return JSON.toJSONString(list);
	}
}